<template>
  <div class="container">
    <div class="screen">
      <div class="screen__content">
        <el-form class="login" @keyup.enter="btnLogin()">
          <div class="login__field">
            <img src="@/assets/icon/user.png" class="login__icon" alt=""/>
            <input
                type="text"
                class="login__input"
                placeholder="User name / Email"
                v-model="username"
            />
          </div>
          <div class="login__field">
            <img src="@/assets/icon/password.png" class="login__icon" alt=""/>
            <el-button
                text
                @click="togglePassword"
                class="login__icon"
                style="left: 240px"
            >
              <i :class="showPassword ? 'el-icon-view' : 'el-icon-view'"></i>
            </el-button>
            <input
                :type="showPassword ? 'text' : 'password'"
                class="login__input"
                placeholder="Password"
                v-model="password"
            />
          </div>
          <el-button
              type="primary"
              class="button login__submit"
              @click="btnLogin()"
          >
            <span class="button__text">Log In Now</span>
            <i class="button__icon fas fa-chevron-right"></i>
          </el-button>
        </el-form>
      </div>
      <div class="screen__background"></div>
    </div>
  </div>
</template>

<script>
import {Login} from '@/api/index'

export default {
  data() {
    return {
      username: '',
      password: '',
      showPassword: false,
    }
  },
  methods: {
    btnLogin() {
      Login({
        username: this.username,
        password: this.password,
      })
          .then((response) => {
            localStorage.setItem('WFITToken', response.data.token)
            this.$store.commit('setRole', response.data.userType)
            this.$store.commit('setUserCampus', response.data.campus)
            if (response.data.userType === 0) {
              this.$router.push('/ConferenceReservations')
            } else {
              this.$router.push('/Dashboard')
            }
            localStorage.setItem('WFITInfo', JSON.stringify(response.data))
            this.$store.dispatch('fetchCampusList')
            this.$store.dispatch('fetchConferenceRoomList')
          })
          .catch((error) => {
            console.error('Login failed:', error)
          })
    },
    togglePassword() {
      this.showPassword = !this.showPassword
    },
  },
}
</script>

<style scoped>
.container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  margin-right: 200px;
}

.screen {
  background: linear-gradient(90deg, #5d54a4, #7c78b8);
  position: relative;
  height: 400px;
  width: 360px;
  box-shadow: 0 0 24px #5c5696;
}

.screen__content {
  box-sizing: border-box;
  z-index: 1;
  position: relative;
  height: 100%;
}

.screen__background {
  background: url('@/assets/icon/bg.png');
  background-size: 380px 620px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
  clip-path: inset(0 0 0 0);
}

.login {
  width: 320px;
  padding: 30px;
  height: 300px;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
}

.login__icon {
  position: absolute;
  width: 37px;
  height: 37px;
  color: #7875b5;
}

.login__input {
  border-radius: 20px;
  border: none;
  border-bottom: 2px solid #d1d1d4;
  background: none;
  padding: 10px;
  padding-left: 44px;
  font-weight: 700;
  width: 60%;
  transition: 0.2s;
}

.login__input:active,
.login__input:focus,
.login__input:hover {
  outline: none;
  border-bottom-color: #6a679e;
}

.login__submit {
  background: #fff;
  font-size: 14px;
  margin-top: 30px;
  padding: 16px 20px;
  border-radius: 26px;
  border: 1px solid #d4d3e8;
  text-transform: uppercase;
  font-weight: 700;
  display: flex;
  align-items: center;
  width: 100%;
  color: #4c489d;
  box-shadow: 0 2px 2px #5c5696;
  cursor: pointer;
  transition: 0.2s;
}

.login__submit:active,
.login__submit:focus,
.login__submit:hover {
  border-color: #6a679e;
  outline: none;
}

.button__icon {
  font-size: 24px;
  margin-left: auto;
  color: #7875b5;
}

</style>
